package com.meixiang.activity.account.myfund;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.ScrollView;

import com.meixiang.R;
import com.meixiang.http.HttpUtils;
import com.meixiang.main.BaseActivity;
import com.meixiang.main.MXApplication;
import com.meixiang.view.TitleView;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by xyj on 2016/6/13.
 * 暂未使用
 */
public class FundDetialViewActivity extends BaseActivity {

    private Context context;
    @Bind(R.id.app_web_title)
    TitleView titleView;
    @Bind(R.id.webview_ground_shop_progressbar)
    ProgressBar progressBar;
    @Bind(R.id.webview_ground_shop_webview)
    WebView webView;
    private String resultUrl;
    private String titleName;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_fund_webview);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
    }


    @Override
    protected void initTitle() {
        titleName = getIntent().getStringExtra("titleName");
        titleView.setBackgroundColor(ContextCompat.getColor(this, R.color.fund_money_color));
        if (titleName == null) {
            titleView.setTitle("基金详情");
        } else {
            titleView.setTitle(titleName);
        }
        titleView.setTColor(ContextCompat.getColor(this, R.color.white));
        titleView.setLeftImageButton(R.mipmap.month_3);
        titleView.showLeftButton(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (webView.canGoBack()) {
                    webView.goBack();
                    return;
                } else {
                    finish();
                }
            }
        });
    }

    @Override
    protected void initView() {
        resultUrl = getIntent().getStringExtra("resultUrl");
    }

    public void initWebView() {
        if (Build.VERSION.SDK_INT >= 20) {
            webView.getSettings().setLoadsImagesAutomatically(true);
        } else {
            webView.getSettings().setLoadsImagesAutomatically(false);
        }
        webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); // 设置默认方式
        webView.getSettings().setJavaScriptEnabled(true); // 设置是否支持javaScript
        webView.getSettings().setAllowFileAccess(true);
        webView.getSettings().setBlockNetworkImage(true);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setUserAgentString("MXSH1.0");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        }
    }

    @Override
    protected void setData() {
        initWebView();
        webView.setWebViewClient(new WebViewClientImpl());
        webView.setWebChromeClient(new WebChromeClientImpl());
        webView.loadUrl(resultUrl, HttpUtils.getHeader().headersMap);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                if (webView.canGoBack()) { //如果webView可以返回就返回上一个页面
                    webView.goBack();
                    return true;
                }
                break;

            default:
                break;
        }

        return super.onKeyDown(keyCode, event);
    }

    private class WebChromeClientImpl extends WebChromeClient {
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
            if (newProgress == 100) {
                progressBar.setVisibility(View.GONE);
            } else {
                if (View.INVISIBLE == progressBar.getVisibility()) {
                    progressBar.setVisibility(View.VISIBLE);
                }
                progressBar.setProgress(newProgress);
            }
            super.onProgressChanged(view, newProgress);
        }
    }

    private class WebViewClientImpl extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            progressBar.setVisibility(View.VISIBLE);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            if (!webView.getSettings().getLoadsImagesAutomatically()) {
                webView.getSettings().setLoadsImagesAutomatically(true);
            }
            super.onPageFinished(view, url);
        }

        @SuppressWarnings("deprecation")
        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            view.loadUrl("file:///android_asset/404.html"); //加载出错页面
        }
    }

    @Override
    public void onClick(View v) {

    }
}
